﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

using NUnit.Framework;

using Refix.Core;

namespace Refix.Integration.Tests
{
    [TestFixture]
    public class RakeTest
    {
        private string _solutionRootDirectory;
        private string _repoPath;
        private string _refixPath;

        [TestFixtureSetUp]
        public void TextFixtureSetUp()
        {
            var fileInfo = new FileInfo(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
            _solutionRootDirectory = fileInfo.Directory.Parent.Parent.Parent.FullName;
            _repoPath = Path.Combine(_solutionRootDirectory, ".testrefixrepo");
            Environment.SetEnvironmentVariable(EnvironmentVariables.RefixLocalRepository, _repoPath,
                                               EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable(EnvironmentVariables.RefixRemoteRepository, null,
                                               EnvironmentVariableTarget.Process);
#if DEBUG
            _refixPath = Path.Combine(_solutionRootDirectory, "Refix.Console\\bin\\Debug\\rfx.exe");
#else
            _refixPath = Path.Combine(_solutionRootDirectory, "Refix.Console\\bin\\Release\\rfx.exe");
#endif
        }

        [Test]
        [Category("Integration")]
        [Ignore("Flaky for some reason - to revisit")]
        public void Rake_Build_Fails_Then_Is_Fixed_With_Alt()
        {
            Process process = Process.Start(new ProcessStartInfo("rake")
                                                {
                                                    WorkingDirectory =
                                                        Path.Combine(_solutionRootDirectory, "Refix.TestSolution")
                                                });
            process.WaitForExit(30000);
            Assert.AreNotEqual(0, process.ExitCode, "Process should have failed before the alt command was run.");
            process = Process.Start(new ProcessStartInfo(_refixPath, "alt Refix.Test.A 0.1.0.2 0.1.0.2+")
                                        {
                                            WorkingDirectory =
                                                Path.Combine(_solutionRootDirectory, "Refix.TestSolution")
                                        });
            process.WaitForExit(30000);
            process = Process.Start(new ProcessStartInfo("rake")
                                        {
                                            WorkingDirectory =
                                                Path.Combine(_solutionRootDirectory, "Refix.TestSolution")
                                        });
            process.WaitForExit(30000);
            Assert.AreEqual(0, process.ExitCode, "Process should have succeeded after the alt command was run.");
            process = Process.Start(new ProcessStartInfo(_refixPath, "alt -d Refix.Test.A 0.1.0.2 0.1.0.2+")
                                        {
                                            WorkingDirectory =
                                                Path.Combine(_solutionRootDirectory, "Refix.TestSolution")
                                        });
            process.WaitForExit(30000);
            process = Process.Start(new ProcessStartInfo(_refixPath, "clean -a Refix.TestSolution.sln")
            {
                WorkingDirectory =
                    Path.Combine(_solutionRootDirectory, "Refix.TestSolution")
            });
            process.WaitForExit(30000);
        }
    }
}